package com.lin.views;



import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import com.lin.utils.Parameters;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.PinterestCircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.FriendsShareBean;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.AbsListView.OnScrollListener;

public class NiceGirlViewPager  extends ViewPager {

	private SyncImageLoaderForGif mSyncImageLoader;
	Activity mActivity;
	ItemAdapter mItemAdapter;
	DisplayImageOptions options;

//	byte[] buffer;
	public NiceGirlViewPager(Activity context) {
		super(context);
		// TODO Auto-generated constructor stub
		mActivity = context;
		options = new DisplayImageOptions.Builder()
		/*			.showImageOnFail(R.drawable.default_avatar)
		.showImageOnLoading(R.drawable.default_avatar)*/
		.displayer(new PinterestCircleBitmapDisplayer(20))
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();
	}


	public NiceGirlViewPager(Activity context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mActivity = context;
		options = new DisplayImageOptions.Builder()
		/*			.showImageOnFail(R.drawable.default_avatar)
		.showImageOnLoading(R.drawable.default_avatar)*/
		.displayer(new PinterestCircleBitmapDisplayer(20))
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();
	}


	public void setData(FriendsShareBean myListInfo) {
		if(mItemAdapter == null) {
			mItemAdapter = new ItemAdapter(myListInfo);
			setAdapter(mItemAdapter);
		} else {
			mItemAdapter.replaceMyListInfoArrayList(myListInfo);
		}
	}



	public class ItemAdapter extends PagerAdapter {

		private FriendsShareBean mFriendsShareBean;
		private LayoutInflater mInflater;



		public ItemAdapter(FriendsShareBean mFriendsShareBean) {
			super();
			this.mFriendsShareBean = mFriendsShareBean;
			this.mInflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			mSyncImageLoader = new SyncImageLoaderForGif(mActivity);
		}


		@Override
		public int getCount() {
			return this.mFriendsShareBean.getPhotoList().size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == ((RelativeLayout) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
//			TouchImageView imgDisplay;
			//			Button btnClose;

			mInflater = (LayoutInflater) mActivity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View viewLayout = mInflater.inflate(R.layout.layout_fullscreen_image, container,
					false);

			imgDisplay = (ImageView) viewLayout.findViewById(R.id.imgDisplay);

			mLoadingImageView = (ImageView) viewLayout.findViewById(R.id.main_utils_loading_img);
			
			//			
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inPreferredConfig = Bitmap.Config.ARGB_8888;

			viewLayout.setTag(position);
			if (mFriendsShareBean.getPhotoList().get(position) != null) {
//				FriendsShareBean model = mFriendsShareBean.get(position);
//				model.setImageUrl("http://7pic.unweixin.com/2013/08-10/B-135713748.gif");
//				mSyncImageLoader.loadImage(position,
//						Parameters.SERVER_PIC_URL + mFriendsShareBean.getPhotoList().get(position), imageLoadListener);
				
				ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + mFriendsShareBean.getPhotoList().get(position), imgDisplay, new ImageLoadingListener() {

					@Override
					public void onLoadingStarted(String imageUri, View view) {
						// TODO Auto-generated method stub
						AnimationManger.startAnimation(mLoadingImageView, mActivity);			
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage, boolean isLoadedFromInternet) {
						// TODO Auto-generated method stub
						imgDisplay.setVisibility(View.VISIBLE);
						mLoadingImageView.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingCancelled(String imageUri, View view) {
						// TODO Auto-generated method stub
						
					}
					
				});
			}
			

			((ViewPager) container).addView(viewLayout);

			return viewLayout;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeView((RelativeLayout) object);

		}

		public void replaceMyListInfoArrayList (FriendsShareBean myListInfo) {
			notifyDataSetChanged();
		}

		private ImageView mLoadingImageView;
		private ImageView imgDisplay;

	}

	public void loadImage() {
		int start = this.getCurrentItem();
		int end = this.getCurrentItem();
		//		if (end >= getCount()) {
		//			end = getCount() - 1;
		//		}
		mSyncImageLoader.setLoadLimit(start, end);
		mSyncImageLoader.unlock();
	}
}